package com.spring.framwork.spring.enty;

import com.spring.framwork.spring.annotation.Component;
import com.spring.framwork.spring.annotation.PostConstruct;
import com.spring.framwork.spring.annotation.Autowried;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class BeanDefinition {
    private final String name;
    private Constructor<?> constructor;
    private  Method postConstructor;
    private List<Field> autowiredFields;
    private Class<?> beanType;
    public BeanDefinition(Class<?> type){
        this.beanType=type;
        Component component = type.getDeclaredAnnotation(Component.class);
        this.name=component.name().isEmpty()? type.getSimpleName(): component.name();
        try {
            this.constructor=type.getConstructor();
            this.postConstructor=Arrays.stream(type.getDeclaredMethods()).filter(c->c.isAnnotationPresent(PostConstruct.class)).findFirst().orElse(null);
            this.autowiredFields=Arrays.stream(type.getDeclaredFields()).filter(c->c.isAnnotationPresent(Autowried.class)).toList();
        } catch (NoSuchMethodException e) {

        }
    }
    public String getName(){
        return name;
    }
    public Constructor<?> getConstructor(){
        return constructor;
    }
    public Method getPostConstructor(){
        return this.postConstructor;
    }
    public List<Field> getAutowiredFields(){
        return this.autowiredFields;
    }
    public Class<?> getBeanType(){
        return this.beanType;
    }
}
